/**
 * Created by 91336 on 13-10-30.
 */
var currTab = 0;
var menu = [];
$(function(){
    initTopTab();
})

/**
 * 获取菜单数据
 * @param parentId
 * @returns {*}
 */
function getMenuData(parentId){
    if(!menu[parentId]){
        $.getJSON($('#nav').attr('data-url'),{parent: parentId}, function(data){
            menu[parentId] = data;
        });
    }
    return menu[parentId];
}

/**
 * 初始化头部菜单
 */
function initTopTab(){
    $.each(getMenuData(0), function(k, v){
        var item = $('<li><a class="link" href="javascript:;" id="tab_' + v.menu_id + '">' + v.menu_name + '</a></li>');
        if(!currTab) currTab = v.menu_id;
        item.children('a').click(
            function(){
                var tabName = this.id.substr(4);
                if(tabName == currTab) return;
                switchTab(tabName);
                openItem();
            }
        );
        $('#nav').append(item);
    });
    /* 切换到默认选项卡 */
    switchTab(currTab);
    openItem();
}

/**
 * 菜单切换
 * @param tabName
 */
function switchTab(tabName){
    currTab = tabName;
    $("#leftMenuBox").panel({title: $('#tab_' + currTab).text()});
    pickTab();
    loadSubmenu();
}

/**
 * 改变菜单样式
 */
function pickTab(){
    var id = '#tab_' + currTab;
    $('#nav').find('a').each(function(){
        $(this).removeClass('actived');
        $(this).addClass('link');
    });
    $(id).addClass('actived');
}

/**
 * 初始化二级菜单
 */
function loadSubmenu(){
    var m = getMenuData(currTab);
    /* 删除所有现有子菜单 */
    $('#submenu').find('dd').remove();
    if(!m) return;
    /* 将子菜单逐项添加到菜单中 */
    $.each(m, function(k, v){
        var p = v.parent_id ? v.parent_id : currTab;
        var item = $('<dd><a href="javascript:;" url="' + v.href + '" parent="' + p + '" id="item_' + v.menu_id + '">' + v.menu_name + '</a></dd>');
        item.children('a').click(function(){
            openItem(this.id.substr(5));
        });
        $('#submenu').append(item);
    });
}

function openItem(itemIndex, tab){
    var rightTabs = $('#rightTabs');
    if(typeof(itemIndex) == 'undefined'){
        return;
    }
    var id      = '#item_' + itemIndex;
    if(tab){
        var parent = tab;
    }else{
        var parent  = $(id).attr('parent');
    }
    /* 若不在当前选项卡内 */
    if(parent != currTab){
        /* 切换到指定选项卡 */
        switchTab(parent);
    }
    /* 高亮当前项 */
    $('#submenu').find('a').each(function(){
        $(this).removeClass('selected');
    });
    $(id).addClass('selected');

    /* 更新iframe的内容 */
    var tabName = $(id).text();
    var tabHref = $(id).attr('url');
    if(rightTabs.tabs('exists', tabName)){
        rightTabs.tabs('select', tabName);
    }else{
        $('#rightTabs').tabs('add',{
            title: tabName,
            closable:true,
            href: tabHref,
            fit: true,
            tools:[{
                iconCls:'icon-mini-refresh',
                handler:function(){
                    reflashTab($('#rightTabs'));
                }
            }]
        });
    }
}